package com.tanhua.management.controller;


import com.tanhua.management.service.AnalysisService;
import com.tanhua.management.vo.AnalysisDistributionVo;
import com.tanhua.management.vo.AnalysisSummaryVo;
import com.tanhua.management.vo.AnalysisUsersVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@RequestMapping("/dashboard")
public class AnalysisController {

    @Autowired
    private AnalysisService analysisService;
    /**
     * 注册用户发布，行业top、年龄、性别、地区
     *
     * @param sd 开始时间
     * @param ed 结束时间
     * @return 注册用户发布，行业top、年龄、性别、地区
     */
    @GetMapping("distribution")
    public ResponseEntity<Object> queryUserDistribution(@RequestParam(name = "sd") Long sd
            , @RequestParam("ed") Long ed) {
        try {
            AnalysisDistributionVo analysisDistributionVo = this.analysisService.queryUserDistribution(sd, ed);
            return ResponseEntity.ok(analysisDistributionVo);
        } catch (Exception e) {
            log.error("查询出错: " + e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("查询出错!");
    }


    /**
     * 新增，活跃用户，次日留存率
     *
     * @param sd   开始时间
     * @param ed   结束时间
     * @param type 101 新增 102 活跃用户 103 次日留存率
     * @return 新增 or 活跃用户 or 次日留存率
     */
    @GetMapping("users")
    public ResponseEntity<Object> getUsers(@RequestParam(name = "sd") Long sd
            , @RequestParam("ed") Long ed
            , @RequestParam("type") Integer type) {
        try {
            AnalysisUsersVo analysisUsersVo = this.analysisService.queryAnalysisUsersVo(sd, ed, type);
            return ResponseEntity.ok(analysisUsersVo);
        } catch (Exception e) {
            log.error("查询出错: " + e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("查询出错!");
    }

    /**
     *  概要统计信息
     *
     * @return
     */
    @GetMapping("summary")
    public ResponseEntity<Object> querySummary() {
        try {
            AnalysisSummaryVo analysisSummaryVo = this.analysisService.getSummary();
            return ResponseEntity.ok(analysisSummaryVo);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("查询出错: " + e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("查询出错!");
    }
}
